<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class FileDetailForm extends CFormModel
{
    public $fileModel;
    public $renameKey;    public $version;
    public $action;
    public $id;

    private $_pagination;
    private $_path;
    private $_page =0;
    private $_list;

    public function rules()
    {
        return array(
        array('renameKey,action,id,version,downloadName', 'length','max'=>128),
        );
        return $valid;
    }
    public function init(){
        parent::init();
        $this->renameKey="";
        $this->action="";
    }
    
    public function getData(){
        if($this->_list!==null)
        {
            return $this->_list;
        }
        $this->_pagination = new CPagination;
        $dbData = FileMeta::model()->findByAttributes(array('file_path'=>$this->fileModel->filePath, 'meta_key'=>'version'));
        if (empty($dbData))
        {
            return null;
        }
        $dbData = unserialize($dbData['meta_value']);
        $this->_page = count($dbData);

                                        $pageIndex = Yii::app()->request->getParam("page",0);
        $pageIndex = $pageIndex - 1;

                $pageCount = $this->getTotalItemCount()/$this->_pagination->pageSize;
        if($this->getTotalItemCount()%$this->_pagination->pageSize>0){
            $pageCount = $pageCount+1;
        }
                if($pageIndex<0) $pageIndex = 0;
        if($pageIndex>$pageCount) $pageIndex = $pageCount;
                $this->_pagination->currentPage=(int)$pageIndex;
        $this->_pagination->itemCount=$this->getTotalItemCount();
        $cur  = $this->_pagination->currentPage*$this->_pagination->pageSize;
        $end  = $cur+10;
        if ($this->_page - $end < 0)
        {
            $end = $this->_page;
        }
                $data = array();
        for ($i =$cur ; $i < $end; $i+=1)
        {
            $item = new FileDetailBase();
                        $item->id           = $dbData[$i]['user_id'];
            $item->userNick     = $dbData[$i]['user_nick'];
            $item->versionID    = $dbData[$i]['version_id'];
            $item->type         = Yii::app()->params['fileAction'][$dbData[$i]['type']];
            $item->createdAt    = $dbData[$i]['datetime'];
            $item->curVersion   = $this->fileModel->versionID;
            $item->path         = $this->fileModel->filePath;
            $item->fileName     = $this->fileModel->fileName;
            array_push($data,$item);
        }

        $this->_list = $data;
        return $this->_list;
    }

    private function getTotalItemCount()
    {
        return $this->_page;
    }
    
    public function getPagination(){
        return $this->_pagination;
    }


    
    public function updateFormAction()
    {

        if(empty($this->action))
        {
            return false;
        }
                                if($this->_updateAction())
        {
            return true;
        }
        return false;
    }

    
    private function _updateAction()
    {
                                $mAction = Yii::app()->getController()->mAction;
                                if ($this->action === 'renameAction')
        {
            Yii::app()->getController()->redirect(Yii::app()->createUrl("adminFilePanel/rename",
            $this->getParamers(array('action'=>$mAction, 'id'=>$this->fileModel->id, 'name'=>$this->renameKey))));
            return true;
        }
                                if ($this->action === 'cancelAction')
        {
            Yii::app()->getController()->redirect(Yii::app()->createUrl("adminFilePanel/".$mAction, array('id'=>$this->fileModel->id)));
            return true;
        }
                                if (strstr($this->action, 'versionAction'))
        {
            Yii::app()->getController()->redirect(Yii::app()->createUrl("adminFilePanel/version", array('action'=>$mAction, 'id'=>$this->fileModel->id, 'version'=>$this->version)));
            return true;
        }
                                if (strstr($this->action, 'downloadAction'))
        {
            $name = empty($this->fileModel->fileName) ? '新文件' : $this->fileModel->fileName;
            Yii::app()->getController()->redirect(Yii::app()->createUrl("adminFilePanel/download", array('action'=>$mAction, 'id'=>$this->fileModel->id, 'version'=>$this->version, 'name'=>$name)));
            return true;
        }
    }
    
    public function getParamers($arr=array())
    {
                                $pageArr = array();
        $page = Yii::app()->request->getParam('page');
        if (isset($page))
        {
            $pageArr['page'] = $page;
        }

        return array_merge($pageArr,$arr);
    }

}
